package cz.cuni.amis.episodic.bayes.utils.chart;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import lombok.AllArgsConstructor;
import lombok.Cleanup;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author ik
 */
public class PngGraphDevice extends GraphPaintingDevice {

    File targetDirectory;

    
    public PngGraphDevice(String targetDirectory) {
        this(new File(targetDirectory));
        width = 939;
        height = 275;
    }

    public PngGraphDevice(File targetDirectory) {
        super(targetDirectory);
    }
	public PngGraphDevice(File rootDirectory, int width, int height) {
		super(rootDirectory, width, height);
	}
    @Override
    public void outputGraph(JFreeChart graph, File dir, String name) throws Exception {
        dir.mkdirs(); // TODO this could be done only once
    	@Cleanup
        OutputStream os = new FileOutputStream(dir.getAbsolutePath() + "/" + name + ".png");
        ChartUtilities.writeChartAsPNG(os, graph, width, height);
        os.close();
    }
}
